#include "normaluser.h"

Normaluser::Normaluser(std::string username)
{
    this->username = username;
    std::fstream file;
    file.open(LG_PATH, std::ios::in);
    while(!file.eof())
    {
        std::string line;
        getline(file, line);
        std::vector<std::string> temp = split(line, ' ');
        if(temp[0] == username)
        {
            this->userid = temp[2];
            break;
        }
    }
}

int Normaluser :: drawChangePasswordStart()
{
    
    system("clear");
    std::string psold, psnew, psconfirm;

    std::cout << "\033[01;35m--------------------------------------------------------------" << std::endl;
    std::cout << "|                  Student Mannage System                    |" << std::endl;
    std::cout << "--------------------------------------------------------------" << std::endl;
    std::cout << "|                      Change Password                       |" << std::endl;
    std::cout << "--------------------------------------------------------------" << std::endl;
    std::cout << "|\033[01;32mPassword:                                                   \033[01;35m|" << std::endl;
    std::cout << "--------------------------------------------------------------" << std::endl;
    std::cout << "|Press Enter To Continue...                                  |" << std::endl;
    std::cout << "--------------------------------------------------------------\033[0m" << std::endl;
    moveCursor('u', 4);
    moveCursor('r', 10);
    char *old = getpass("");
    for(int i = 0; i < strlen(old); i++) psold.append(1, old[i]);

    if(!varify(psold)) return 0;

    drawChangePassword();
    
    moveCursor('u', 6);
    moveCursor('r', 14);

    char *nw = getpass("");
    for(int i = 0; i < strlen(nw); i++) psnew.append(1, nw[i]);

    moveCursor('d', 1);
    moveCursor('r', 18);

    char *cf = getpass("");
    for(int i = 0; i < strlen(cf); i++) psconfirm.append(1, cf[i]);

    if(psnew != psconfirm) return 1;
    else changePassword(psnew);
    
    return 2;
}

int Normaluser :: drawSelectSubjectStart()
{
    system("clear");
    std::vector<std::string> title_list, tail, subjects;
    std::vector<std::vector<std::string> > res(6), all_subjects;
    std::map<std::string, bool> selected_subject;
    title_list.push_back("Student Manage System");
    title_list.push_back("Select Subjects");
    res[0].push_back("SN");
    res[1].push_back("Subject Title");
    res[2].push_back("Subject Nature");
    res[3].push_back("Total Classes");
    res[4].push_back("Credits");
    res[5].push_back("Remained");

    std::fstream file;
    file.open(STU_PATH, std::ios::in);
    while(!file.eof())
    {
        std::string line;
        getline(file, line);
        std::vector<std::string> temp = split(line, ' ');
        if(temp[0] == userid)
        {
            subjects = split(temp[3], ';');
            break;
        }
    }
    file.close();
    for(auto a : subjects)
    {
        selected_subject[a] = true;
    }

    all_subjects = readFile(SUB_PATH);
    for(int i = 1; i < all_subjects[0].size(); i++)
    {
        if(!selected_subject[all_subjects[1][i]] && (isDigit(all_subjects[6][i]) > 0)) 
        {
            for(int j = 0; j < all_subjects.size(); j++)
            {
                if(j == all_subjects.size() - 2) continue;
                if(j == all_subjects.size() - 1)
                {
                    res[j - 1].push_back(all_subjects[j][i]);
                }
                else res[j].push_back(all_subjects[j][i]);
            }
        }
        
    }

    if(res[0].size() <= 1) return -1;

    tail.push_back("Type The SN Code Of Your Ideal Subject:");
    tail.push_back("Press Enter To Go Back...");

    int max_[6] = {0};
    int _max = 1;
    for(int i = 0; i < 6; i++)
    {
        for(int j = 0; j < res[i].size(); j++)
        {
            max_[i] = std::max(max_[i], (int)res[i][j].size());
        }
        max_[i] += 6;
        _max += max_[i] + 1;
    }
    for (int i = 0; i < title_list.size(); i++)
    {
        for (int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;

        std::cout << "\033[01;35m|\033[0m";
        for (int j = 0; j < ((_max - 2) / 2) - (title_list[i].size() / 2); j++) std::cout << " ";
        
        std::cout << "\033[01;35m" << title_list[i] << "\033[0m";
        for (int j = 0; j < ((_max - 2) - (_max - 2) / 2) - (title_list[i].size() - title_list[i].size() / 2); j++) std::cout << " ";
                
        std::cout << "\033[01;35m|\033[0m" << std::endl;
    }
    for (int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";
    std::cout << std::endl;

    for(int i = 0; i < res[1].size(); i++)
    {
        for(int j = 0; j < res.size(); j++)
        {
            std::cout << "\033[01;35m|\033[0m";
            for (int k = 0; k < (max_[j] / 2) - (res[j][i].size() / 2); k++) std::cout << " ";
                
            std::cout << "\033[01;32m" << res[j][i] << "\033[0m";
            for (int k = 0; k < (max_[j] - max_[j] / 2) - (res[j][i].size() - res[j][i].size() / 2); k++)
            std::cout << " ";
        }
        std::cout << "\033[01;35m|\033[0m";
        std::cout << std::endl;
        for (int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;
    }
        

    std::cout << "\033[01;35m|\033[01;32m";
    std::cout << tail[0];
    for(int i = 0; i < (_max - 2 - tail[0].size()); i++) std::cout << " ";
    std::cout << "\033[01;35m|\033[0m" << std::endl;
    for(int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";
    std::cout << std::endl;

    std::cout << "\033[01;35m|";
    std::cout << tail[1];
    for(int i = 0; i < (_max - 2 - tail[1].size()); i++) std::cout << " ";
    std::cout << "\033[01;35m|\033[0m" << std::endl;
    for(int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";

    std::cout << "\033[01;32m" << std::endl;

    moveCursor('u', 4);
    moveCursor('r', 40);

    std::cout << "\033[01m";

    std::string selected_sn = readCommand();

    if(selected_sn == "q") return -2;
    bool is_exist = false;
    for(int i = 0; i < res[0].size(); i++)
    {
        if(res[0][i] == selected_sn)
        {
            is_exist = true;
            break;
        }
    }
    if(!is_exist) return 0;

    int added_credit;
    std::string sub_title;

    std::vector< std::vector<std::string> > sub_info = readFile(SUB_PATH);
    for(int i = 0; i < sub_info.size(); i++) sub_info[i].erase(sub_info[i].begin());
    for(int i = 0; i < sub_info[0].size(); i++)
    {
        if(sub_info[0][i] == selected_sn)
        {
            sub_info[5][i] = std::to_string(isDigit(sub_info[5][i]) + 1);
            sub_info[6][i] = std::to_string(isDigit(sub_info[6][i]) - 1);
            added_credit = isDigit(sub_info[4][i]);
            sub_title = sub_info[1][i];
            break;
        }
    }

    file.open(SUB_PATH, std::ios::out);
    
    for(int i = 0; i < sub_info[0].size(); i++)
    {
        std::string line;
        for(int j = 0; j < sub_info.size(); j++)
        {
            line.append("\"" + sub_info[j][i] + "\"");
            if(j!= sub_info.size() - 1) line.append(" ");
        }
        file << line << std::endl;
    }
    file.close();

    std::vector< std::vector<std::string> > stu_info = readFile(STU_PATH);
    stu_info.erase(stu_info.begin());
    for(int i = 0; i < stu_info.size(); i++) stu_info[i].erase(stu_info[i].begin());
    for(int i = 0; i < stu_info[0].size(); i++)
    {
        if(stu_info[0][i] == userid)
        {
            if(stu_info[3][i] != "") stu_info[3][i].append(";");
            stu_info[3][i].append(sub_title);
            stu_info[4][i] = std::to_string(isDigit(stu_info[4][i]) + added_credit);
            break;
        }
    }
    
    file.open(STU_PATH, std::ios::out);
    
    for(int i = 0; i < stu_info[0].size(); i++)
    {
        std::string line;
        for(int j = 0; j < stu_info.size(); j++)
        {
            line.append("\"" + stu_info[j][i] + "\"");
            if(j!= stu_info.size() - 1) line.append(" ");
        }
        file << line << std::endl;
    }
    file.close();
    return 1;
    
}

int Normaluser::drawDropSubjectStart()
{
    system("clear");
    std::vector<std::string> title_list, tail, subjects;
    std::vector<std::vector<std::string> > res(5), all_subjects;
    std::map<std::string, bool> selected_subject;
    title_list.push_back("Student Manage System");
    title_list.push_back("Drop Subjects");
    res[0].push_back("SN");
    res[1].push_back("Subject Title");
    res[2].push_back("Subject Nature");
    res[3].push_back("Total Classes");
    res[4].push_back("Credits");

    std::fstream file;
    file.open(STU_PATH, std::ios::in);
    while(!file.eof())
    {
        std::string line;
        getline(file, line);
        std::vector<std::string> temp = split(line, ' ');
        if(temp[0] == userid)
        {
            subjects = split(temp[3], ';');
            break;
        }
    }
    file.close();
    for(auto a : subjects)
    {
        selected_subject[a] = true;
    }

    all_subjects = readFile(SUB_PATH);
    for(int i = 1; i < all_subjects[0].size(); i++)
    {
        if(selected_subject[all_subjects[1][i]]) 
        {
            for(int j = 0; j < all_subjects.size() - 2; j++)
            {
                res[j].push_back(all_subjects[j][i]);
            }
        }
        
    }

    if(res[0].size() <= 1) return -1;

    tail.push_back("Type The SN Code Of Which You Want To Drop:");
    tail.push_back("Press Enter To Go Back...");

    int max_[5] = {0};
    int _max = 1;
    for(int i = 0; i < 5; i++)
    {
        for(int j = 0; j < res[i].size(); j++)
        {
            max_[i] = std::max(max_[i], (int)res[i][j].size());
        }
        max_[i] += 6;
        _max += max_[i] + 1;
    }

    std::cout << std::endl;
    for (int i = 0; i < title_list.size(); i++)
    {
        for (int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;

        std::cout << "\033[01;35m|\033[0m";
        for (int j = 0; j < ((_max - 2) / 2) - (title_list[i].size() / 2); j++) std::cout << " ";
        
        std::cout << "\033[01;35m" << title_list[i] << "\033[0m";
        for (int j = 0; j < ((_max - 2) - (_max - 2) / 2) - (title_list[i].size() - title_list[i].size() / 2); j++) std::cout << " ";
                
        std::cout << "\033[01;35m|\033[0m" << std::endl;
    }
    for (int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";
    std::cout << std::endl;

    for(int i = 0; i < res[1].size(); i++)
    {
        for(int j = 0; j < res.size(); j++)
        {
            std::cout << "\033[01;35m|\033[0m";
            for (int k = 0; k < (max_[j] / 2) - (res[j][i].size() / 2); k++) std::cout << " ";
            std::cout << "\033[01;32m" << res[j][i] << "\033[0m";
            for (int k = 0; k < (max_[j] - max_[j] / 2) - (res[j][i].size() - res[j][i].size() / 2); k++)
            std::cout << " ";
        }
        std::cout << "\033[01;35m|\033[0m";
        std::cout << std::endl;
        for (int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;
    }
        

    std::cout << "\033[01;35m|\033[01;32m";
    std::cout << tail[0];
    for(int i = 0; i < (_max - 2 - tail[0].size()); i++) std::cout << " ";
    std::cout << "\033[01;35m|\033[0m" << std::endl;
    for(int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";
    std::cout << std::endl;

    std::cout << "\033[01;35m|";
    std::cout << tail[1];
    for(int i = 0; i < (_max - 2 - tail[1].size()); i++) std::cout << " ";
    std::cout << "\033[01;35m|\033[0m" << std::endl;
    for(int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";

    std::cout << "\033[01;32m" << std::endl;

    moveCursor('u', 4);
    moveCursor('r', 44);

    std::cout << "\033[01m";

    std::string selected_sn = readCommand();

    if(selected_sn == "q") return -2;

    bool is_exist = false;
    for(int i = 0; i < res[0].size(); i++)
    {
        if(res[0][i] == selected_sn)
        {
            is_exist = true;
            break;
        }
    }
    if(!is_exist) return 0;

    int minused_credit;
    std::string sub_title;

    std::vector< std::vector<std::string> > sub_info = readFile(SUB_PATH);
    for(int i = 0; i < sub_info.size(); i++) sub_info[i].erase(sub_info[i].begin());
    for(int i = 0; i < sub_info[0].size(); i++)
    {
        if(sub_info[0][i] == selected_sn)
        {
            sub_info[5][i] = std::to_string(isDigit(sub_info[5][i]) - 1);
            sub_info[6][i] = std::to_string(isDigit(sub_info[6][i]) + 1);
            minused_credit = isDigit(sub_info[4][i]);
            sub_title = sub_info[1][i];
            break;
        }
    }

    file.open(SUB_PATH, std::ios::out);
    
    for(int i = 0; i < sub_info[0].size(); i++)
    {
        std::string line;
        for(int j = 0; j < sub_info.size(); j++)
        {
            line.append("\"" + sub_info[j][i] + "\"");
            if(j!= sub_info.size() - 1) line.append(" ");
        }
        file << line << std::endl;
    }
    file.close();

    std::vector< std::vector<std::string> > stu_info = readFile(STU_PATH);
    stu_info.erase(stu_info.begin());
    for(int i = 0; i < stu_info.size(); i++) stu_info[i].erase(stu_info[i].begin());
    for(int i = 0; i < stu_info[0].size(); i++)
    {
        if(stu_info[0][i] == userid)
        {
            std::vector<std::string> tempvec = split(stu_info[3][i], ';');
            std::string final_sub;
            for(int j = 0; j < tempvec.size(); j++)
            {
                if(tempvec[j] != sub_title)
                {
                    final_sub.append(tempvec[j]);
                    if(j != tempvec.size() - 1) final_sub.append(";");
                }
            }
            stu_info[3][i] = final_sub;
            stu_info[4][i] = std::to_string(isDigit(stu_info[4][i]) - minused_credit);
            break;
        }
    }
    
    file.open(STU_PATH, std::ios::out);
    
    for(int i = 0; i < stu_info[0].size(); i++)
    {
        std::string line;
        for(int j = 0; j < stu_info.size(); j++)
        {
            line.append("\"" + stu_info[j][i] + "\"");
            if(j!= stu_info.size() - 1) line.append(" ");
        }
        file << line << std::endl;
    }
    file.close();
    return 1;
    
}

void Normaluser :: drawChangePassword()
{
    system("clear");
    std::cout << "\033[01;35m--------------------------------------------------------------" << std::endl;
    std::cout << "|                  Student Mannage System                    |" << std::endl;
    std::cout << "--------------------------------------------------------------" << std::endl;
    std::cout << "|                      Change Password                       |" << std::endl;
    std::cout << "--------------------------------------------------------------" << std::endl;
    std::cout << "|\033[01;32mNew Password:                                               \033[01;35m|" << std::endl;
    std::cout << "--------------------------------------------------------------" << std::endl;
    std::cout << "|\033[01;32mConfirm Password:                                           \033[01;35m|" << std::endl;
    std::cout << "--------------------------------------------------------------" << std::endl;
    std::cout << "|Press Enter To Continue...                                  |" << std::endl;
    std::cout << "--------------------------------------------------------------\033[0m" << std::endl;
}

void Normaluser :: drawMySubectsStart()
{
    system("clear");
    std::vector<std::string> title_list, tail, subjects;
    std::vector<std::vector<std::string> > res(5);
    title_list.push_back("Student Manage System");
    title_list.push_back("My Subjects");
    res[0].push_back("SN");
    res[1].push_back("Subject Title");
    res[2].push_back("Subject Nature");
    res[3].push_back("Total Classes");
    res[4].push_back("Credits");
    std::fstream file;
    file.open(STU_PATH, std::ios::in);
    while(!file.eof())
    {
        std::string line;
        getline(file, line);
        std::vector<std::string> temp = split(line, ' ');
        if(temp[0] == userid)
        {
            tail.push_back("Total Credits: " + temp[4]);
            subjects = split(temp[3], ';');
            break;
        }
    }
    file.close();
    for(auto a : subjects)
    {
        for(int i = 0; i < res.size(); i++)
        {
            if(i == 1) res[1].push_back(a);
            else res[i].push_back("");
        }
    }
    file.open(SUB_PATH, std::ios::in);

    while(!file.eof())
    {
        std::string line;
        getline(file, line);
        std::vector<std::string> temp = split(line,' ');
        for(int i = 0; i < res[1].size(); i++)
        {
            if(temp[1] == res[1][i])
            {
                
                for(int j = 0; j < res.size(); j++)
                {
                    if(j == 1) continue;
                    res[j][i] = temp[j];
                }
            }
        }
    }
    file.close();

    tail.push_back("Press Enter To Go Back...");

    int max_[5] = {0};
    int _max = 1;
    for(int i = 0; i < 5; i++)
    {
        for(int j = 0; j < res[i].size(); j++)
        {
            max_[i] = std::max(max_[i], (int)res[i][j].size());
        }
        max_[i] += 6;
        _max += max_[i] + 1;
    }
    for (int i = 0; i < title_list.size(); i++)
    {
        for (int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;

        std::cout << "\033[01;35m|\033[0m";
        for (int j = 0; j < ((_max - 2) / 2) - (title_list[i].size() / 2); j++) std::cout << " ";
        
        std::cout << "\033[01;35m" << title_list[i] << "\033[0m";
        for (int j = 0; j < ((_max - 2) - (_max - 2) / 2) - (title_list[i].size() - title_list[i].size() / 2); j++) std::cout << " ";
                
        std::cout << "\033[01;35m|\033[0m" << std::endl;
    }
    for (int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";
    std::cout << std::endl;

    for(int i = 0; i < res[1].size(); i++)
    {
        for(int j = 0; j < res.size(); j++)
        {
            std::cout << "\033[01;35m|\033[0m";
            for (int k = 0; k < (max_[j] / 2) - (res[j][i].size() / 2); k++) std::cout << " ";
                
            std::cout << "\033[01;32m" << res[j][i] << "\033[0m";
            for (int k = 0; k < (max_[j] - max_[j] / 2) - (res[j][i].size() - res[j][i].size() / 2); k++)
            std::cout << " ";
        }
        std::cout << "\033[01;35m|\033[0m";
        std::cout << std::endl;
        for (int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;
    }
        

    std::cout << "\033[01;35m|\033[01;31m";
    std::cout << tail[0];
    for(int i = 0; i < (_max - 2 - tail[0].size()); i++) std::cout << " ";
    std::cout << "\033[01;35m|\033[0m" << std::endl;
    for(int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";
    std::cout << std::endl;

    std::cout << "\033[01;35m|";
    std::cout << tail[1];
    for(int i = 0; i < (_max - 2 - tail[1].size()); i++) std::cout << " ";
    std::cout << "\033[01;35m|\033[0m" << std::endl;
    for(int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";

    char *p = getpass("");
}
void Normaluser :: changePassword(std::string new_password)
{
    std::vector<std::string> res;
    std::fstream file;
    file.open(LG_PATH, std::fstream::in);
    while(!file.eof())
    {
        std::string line;
        getline(file, line);
        std::vector<std::string> temp = split(line, ' ');
        if(temp[0] != username)
        {
            res.push_back(line);
        }
        else
        {
            temp[1] = new_password;
            std::string t;
            for(int i = 0; i < temp.size(); i++)
            {
                t.append("\"" + temp[i] + "\"");
                if(i != temp.size() - 1) t.append(" ");
            }
            res.push_back(t);
        }
    }
    file.close();
    file.open(LG_PATH, std::fstream::out);
    for(auto a : res)
    {
        file << a << std::endl;
    }
    file.close();
}
bool Normaluser :: varify(std::string password)
{
    std::fstream file;
    file.open(LG_PATH, std::ios::in);
    if(!file.is_open()) return false;
    
    while(!file.eof()) 
    {
        std::string line;
        std::getline(file, line);
        std::vector<std::string> temp = split(line, ' ');
        if(temp[1] == password) return true;
    }
    return false;
}